home
diamond Go Premium
Data Engineering Path  ·  PySpark

DataFrame Select Operation

Selecting columns, projecting values, performing on-the-fly math calculations, and applying SQL expressions in PySpark.


What is the Select Operation?

The select() operation projects a set of expressions or columns, similar to the SELECT clause in relational SQL. In PySpark, it can take raw column names as strings, Column objects, or SQL expressions using selectExpr().

It is a lazy transformation that returns a new DataFrame with only the selected/calculated columns.


Syntax and Column References

You can reference columns in PySpark in three distinct ways:

from pyspark.sql.functions import col

# Method A: String representation (Simpler, but no column transformations allowed)
df.select("name", "salary")

# Method B: Column Object reference (Allows arithmetic and conditional operations)
df.select(col("name"), col("salary") + 5000)

# Method C: SQL Expression string projection (Using selectExpr)
df.selectExpr("name", "salary + 5000 AS new_salary", "upper(department)")

Example Usage Pipeline

Below is a complete, copy-paste-ready PySpark script demonstrating advanced selection techniques:

from pyspark.sql import SparkSession
from pyspark.sql.functions import col, upper, concat_ws

# 1. Setup local Spark session
spark = SparkSession.builder \
    .appName("DataFrame Select Demo") \
    .master("local[*]") \
    .getOrCreate()

# 2. Dummy dataset (Employees)
data = [
    ("usr_001", "John", "Smith", 90000, "Engineering"),
    ("usr_002", "Jane", "Doe", 110000, "Marketing"),
    ("usr_003", "Robert", "Johnson", 75000, "Engineering"),
]
columns = ["emp_id", "first_name", "last_name", "salary", "department"]
df = spark.createDataFrame(data, columns)

# 3. Apply Select Transformations
# - Combine first and last names into a single column
# - Calculate taxed salary
# - Convert department name to uppercase
selected_df = df.select(
    col("emp_id"),
    concat_ws(" ", col("first_name"), col("last_name")).alias("full_name"),
    (col("salary") * 0.85).alias("net_salary"),
    upper(col("department")).alias("dept_upper")
)

# 4. Apply selectExpr for SQL-style on-the-fly calculations
expr_df = df.selectExpr(
    "emp_id",
    "first_name || ' ' || last_name AS full_name",
    "salary * 0.85 AS net_salary",
    "upper(department) AS dept_upper"
)

# 5. Show results
print("=== Output from DSL select() ===")
selected_df.show(truncate=False)

print("=== Output from selectExpr() ===")
expr_df.show(truncate=False)

Rendered Output:

=== Output from DSL select() ===
+-------+--------------+----------+-----------+
|emp_id |full_name     |net_salary|dept_upper |
+-------+--------------+----------+-----------+
|usr_001|John Smith    |76500.0   |ENGINEERING|
|usr_002|Jane Doe      |93500.0   |MARKETING  |
|usr_003|Robert Johnson|63750.0   |ENGINEERING|
+-------+--------------+----------+-----------+

=== Output from selectExpr() ===
+-------+--------------+----------+-----------+
|emp_id |full_name     |net_salary|dept_upper |
+-------+--------------+----------+-----------+
|usr_001|John Smith    |76500.0   |ENGINEERING|
|usr_002|Jane Doe      |93500.0   |MARKETING  |
|usr_003|Robert Johnson|63750.0   |ENGINEERING|
+-------+--------------+----------+-----------+
Find this content helpful? ☕ Buy me a coffee

Entity Details

Create New Item

celebration
Enjoying the free content?

Create a free account to track your progress and save your place.

Create Free Account
help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.